<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CommonService extends MY_Controller {
	/**
     * Summary of __construct
     */
	public function __construct(){
        parent::__construct();
        $this->load->model('User_model', 'User');
        $this->load->model('SMS_model', 'SMS');
    }

    public function getUser(){
        $phone = $this->input->get("phone");

        $users = $this->User->getManyByCondition( '*', array('phone' => $phone), null );
        if(count($users) == 0){
            echo(json_encode(array( "code" => "error")));
        }
        else{
            echo(json_encode(array( "code" => "success")));
        }
    }

    public function join(){
        $sms_id = $this->input->post("sms_id");
        $phone = $this->input->post("phone");
        $sms = $this->input->post("sms");

        $list = $this->SMS->getManyByCondition( '*', array('id' => $sms_id), null );

        if(count($list) == 0 || ($list[0]['sms_code'] != $sms || $list[0]['phone'] != $phone)){
            echo(json_encode(array( "code" => "error", "message" => '验证码错误' )));
        }
        else{
            $data = array(
                    "user_name" => $this->input->post("user_name"),
                    "phone" => $this->input->post("phone"),
                    "pass" => '',
                    'role' => 'common',
                    "status" => 0,
                    'add_time' => date("Y-m-d H:i:s"),
                    'last_update_time' => date("Y-m-d H:i:s")
                );
            $this->User->add( $data );

            echo(json_encode(array( "code" => "success")));
        }
    }
}
